﻿using Prism.Commands;
using Prism.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;

namespace Shell.ViewModels
{
    public class MyDialogViewModel :BindableBase, IDialogAware
    {
        // 关键点：RequestClose 是属性而非事件
        public DialogCloseListener RequestClose { get; set; }

        // 确定命令
        public DelegateCommand OkCommand { get; }
        // 取消命令
        public DelegateCommand CancelCommand { get; }

        public MyDialogViewModel()
        {
            OkCommand = new DelegateCommand(() => CloseDialog(ButtonResult.OK));
            CancelCommand = new DelegateCommand(() => CloseDialog(ButtonResult.Cancel));
        }
        private string _message;
        public string Message
        {
            get => _message;
            set => SetProperty(ref _message, value);
        }

        private void CloseDialog(ButtonResult result)
        {
            // 通过 RequestClose 属性触发关闭
            RequestClose.Invoke(new DialogResult(result));
        }

        // 实现接口方法
        public bool CanCloseDialog() => true;
        public void OnDialogClosed() { }
        public void OnDialogOpened(IDialogParameters parameters)
        {

            if (parameters.ContainsKey("dialogMessage"))
            {
                Message = parameters.GetValue<string>("dialogMessage");
            }


        }
    }

}
